package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallOrderGoods;
import com.ctsi.ssdc.domain.LitemallOrderGoodsExample;
import com.ctsi.ssdc.service.LitemallOrderGoodsService;
import com.ctsi.ssdc.repository.LitemallOrderGoodsRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallOrderGoods.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallOrderGoodsServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallOrderGoodsRepository, LitemallOrderGoods, Integer, LitemallOrderGoodsExample> 
	implements LitemallOrderGoodsService {

    @Autowired
    LitemallOrderGoodsRepository litemallOrderGoodsRepository;


    /**
    * GET  /litemallOrderGoodss : get the litemallOrderGoodss firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallOrderGoods> findFirstStringColumn(String goodsName,Pageable pageable){
        String str = goodsName;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallOrderGoodsExample litemallOrderGoodsExample = new LitemallOrderGoodsExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallOrderGoodsExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallOrderGoodsExample.createCriteria();
        } else{
            litemallOrderGoodsExample.createCriteria().andGoodsNameLike("%" + str +"%");
        }
        List<LitemallOrderGoods>  data = litemallOrderGoodsRepository.selectByExample(litemallOrderGoodsExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallOrderGoodsRepository.countByExample(litemallOrderGoodsExample);
        }
        return new PageResult<LitemallOrderGoods>(data,count,count);
    }

    @Override
    public List<LitemallOrderGoods> queryByOid(Integer orderId) {
        LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        return litemallOrderGoodsRepository.selectByExample(example);
    }

    @Override
    public int add(LitemallOrderGoods orderGoods) {
        orderGoods.setAddTime(ZonedDateTime.now());
        orderGoods.setUpdateTime(ZonedDateTime.now());
        return litemallOrderGoodsRepository.insertSelective(orderGoods);
    }

    @Override
    public Short getComments(Integer orderId) {
        LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        long count = litemallOrderGoodsRepository.countByExample(example);
        return (short) count;
    }

    @Override
    public List<LitemallOrderGoods> findByOidAndGid(Integer orderId, Integer goodsId) {
        LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return litemallOrderGoodsRepository.selectByExample(example);
    }

    @Override
    public LitemallOrderGoods findById(Integer orderGoodsId) {
        return litemallOrderGoodsRepository.selectByPrimaryKey(orderGoodsId);
    }

    @Override
    public void updateById(LitemallOrderGoods orderGoods) {
        orderGoods.setUpdateTime(ZonedDateTime.now());
        litemallOrderGoodsRepository.updateByPrimaryKeySelective(orderGoods);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
